//
//  ELHomeCellTopView.swift
//  SinaWeiBo
//
//  Created by jiwang on 15/12/20.
//  Copyright © 2015年 jiwang. All rights reserved.
//

import UIKit
import SDWebImage

class ELHomeCellTopView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var status : ELStatus? {
        didSet {
            // 1.显示头像
            if let profile_image_url = status?.user?.profile_image_url {
                let url = NSURL(string: profile_image_url)!
                iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar"))
            }
            
            // 2.用户名称
            nameLabel.text = status?.user?.screen_name
            
            // 3.显示时间 status?.created_at
            timeLabel.text = "刚刚"
            
            // 4.来源
            sourceLabel.text = "iphone7"
            
            // 5.vip等级
            vipView.image = status?.user?.mbrankImage
            
            // 6.认证等级
            verifiedView.image = status?.user?.verifiedImage
        }
    }

    private func prepareUI () {

        // 1.添加子控件
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(vipView)
        addSubview(verifiedView)
        
        //添加约束
        // 头像约束
        iconView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: self, size: CGSize(width: 35, height: 35), offset: CGPoint(x: 8, y: 8))
        
        // 名字约束
        nameLabel.ff_AlignHorizontal(type: ff_AlignType.TopRight, referView: iconView, size: nil, offset: CGPoint(x: 8, y: 0))
        
        // 时间
        timeLabel.ff_AlignHorizontal(type: ff_AlignType.BottomRight, referView: iconView, size: nil, offset: CGPoint(x: 8, y: 0))
        
        // 来源
        sourceLabel.ff_AlignHorizontal(type: ff_AlignType.CenterRight, referView: timeLabel, size: nil, offset: CGPoint(x: 8, y: 0))
        
        // 会员等级
        vipView.ff_AlignHorizontal(type: ff_AlignType.CenterRight, referView: nameLabel, size: CGSize(width: 14, height: 14), offset: CGPoint(x: 8, y: 0))
        
        // 认证图标
        verifiedView.ff_AlignInner(type: ff_AlignType.BottomRight, referView: iconView, size: CGSize(width: 17, height: 17), offset: CGPoint(x: 8.5, y: 8.5))
    }
    
    
    //MARK - 懒加载
    ///用户头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar"))
    
    /// 用户名称
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        
        // 设置字体颜色
        label.textColor = UIColor.darkGrayColor()
        
        // 设置大小
        label.font = UIFont.systemFontOfSize(14)
        
        return label
    }()
    
    /// 微博时间
    private lazy var timeLabel: UILabel = {
        let label = UILabel()
        
        // 设置字体颜色
        label.textColor = UIColor.orangeColor()
        
        // 设置大小
        label.font = UIFont.systemFontOfSize(9)
        
        return label
    }()
    
    /// 微博来源
    private lazy var sourceLabel: UILabel = {
        let label = UILabel()
        
        // 设置字体颜色
        label.textColor = UIColor.lightGrayColor()
        
        // 设置大小
        label.font = UIFont.systemFontOfSize(9)
        
        return label
    }()
    
    /// 会员等级
    private lazy var vipView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_expired"))
    
    /// 认证图标
    private lazy var verifiedView: UIImageView = UIImageView(image: nil)
    
}
